package com.jinniulive.platform.module.live.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.jinniulive.platform.R;
import com.jinniulive.platform.bean.HtmlMsg;
import com.jinniulive.platform.urls.AgreementConfig;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * A simple {@link Fragment} subclass.
 */
public class PublicChatFragment extends Fragment {

    public PublicChatFragment() {
        // Required empty public constructor
    }

    private WebView mWebView;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注册
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_public_chat, container, false);
        mWebView = (WebView) inflate.findViewById(R.id.fragment_public_wv);
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        mWebView.loadUrl("file:///android_asset/RoomNotice.html");
        return inflate;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe
    public void receive(HtmlMsg msg){
        String htmlMsg = msg.htmlMsg;
        int msgType = msg.msgType;
        switch (msgType){
            case AgreementConfig.msg_public_chat:
                showHtml(htmlMsg);
                break;
            default:
                break;
        }

    }

    private void showHtml(String htmlMsg) {
        String html= "javascript:addRoomNotice(\"%s\")";
        String formatHtml = String.format(html, htmlMsg);
        mWebView.loadUrl(formatHtml);
    }

}
